package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppComment;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 动态评论
 */
@Repository
public class AppCommentDao extends BaseDao<AppComment> {

    /**
     * 查询指定动态&正常的评论数据
     * @param dongtaiId 动态id
     * @param splitPage
     * @return
     */
    public List<AppComment> specificUserList(Integer dongtaiId, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_comment where status = 1 and dynamicid = ?  ");
        paramList.add(dongtaiId);
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppComment.class, splitPage);
    }

    public List<AppComment> listByDynamicId(Integer dynamicId) {
        ArrayList<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from app_comment where status = 1 and dynamicid = ? order by createtime desc ");
        paramList.add(dynamicId);
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppComment.class);
    }

    /**
     * 获取指定动态的总评论数
     * @param dongtaiId 动态id
     * @return
     */
    public int plcount(Integer dongtaiId) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select count(*) from app_comment where 1=1  ");
        sbStr.append(" and status = 1 ");
        sbStr.append(" and dynamicid = ? ");
        paramList.add(dongtaiId);
        String sql = sbStr.toString();
        return this.queryListCount(sql, paramList);
    }

    /**
     * 详情
     */
    public AppComment get(Integer id) {
        if (StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_comment where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppComment.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppComment haoYou) {
        if (haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if (id <= 0) {
            return;
        }
        String sql = "delete  from app_comment where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    public void delByUserId(Integer userId) {
        String sql = "delete  from app_comment where userid =" + userId;
        this.del(sql, new ArrayList<>());
    }


    /**
     * 更新状态
     */
    public void updateStatus(Integer id, Integer status) {
        if (id <= 0) {
            return;
        }
        String sql = "update app_comment set status = ?  where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(status);
        paramList.add(id);
        this.del(sql, paramList);
    }


    /**
     * 变更评论点赞数量
     * @param id 评论id
     * @param type 1点赞   2取消
     */
    public void updateZanNum(Integer id, Integer type) {
        if (id <= 0) {
            return;
        }
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuffer sql =new StringBuffer("update app_comment ") ;
        if(type==1){
            sql.append("set num = num+1 ");
        }else{
            sql.append("set num = num-1 ");
        }
        sql.append(" where id = ?");
        paramList.add(id);
        this.del(sql.toString(), paramList);
    }


    /**
     * 更新用户头像
     * @param userid
     * @param pic 头像地址
     */
    public void updateImg(Integer userid,String pic){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(pic)){
            return;
        }
        String sql = " UPDATE app_comment SET pic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql,paramList);
    }
    /**
     * 更新用户昵称
     * @param userid
     * @param nick 昵称
     */
    public void updateNick(Integer userid,String nick){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(nick)){
            return;
        }
        String sql = " UPDATE app_comment SET nick = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    /**
    * 更新用户性别
    * @Param: [userid, sex]
    * @return: void
    * @Author: zhouyunyi
    * @Date: 2021/07/28
    * @Time: 18:16
    */
    public void updateSex(Integer userid,Integer sex){
        if(StringEx.isNull(userid)){
            return;
        }
        if(StringEx.isNull(sex)){
            return;
        }
        String sql = " UPDATE app_comment SET sex = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(sex);
        paramList.add(userid);
        this.update(sql,paramList);
    }

}
